package com.easylinkin.linkappapi.airconditioner.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.airconditioner.entity.ext.RegularTemperatureControlExt;
import com.easylinkin.linkappapi.airconditioner.service.TimingTemperatureControlService;
import com.easylinkin.linkappapi.common.model.RequestModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.util.List;

/**
 * class info :定时温控
 *
 * @author liuqihang
 * @date 2021/5/8 18:16
 */
@RestController
@RequestMapping("/timingTemperatureControl")
public class TimingTemperatureControlController {

    @Resource
    private TimingTemperatureControlService service;

    @PostMapping("add")
    public RestMessage add(@RequestBody RegularTemperatureControlExt rtce) {
        Assert.isTrue(!StringUtils.isEmpty(rtce.getName()), "名称不能为空");
        Assert.isTrue(!(rtce.getDeviceIdList() == null || rtce.getDeviceIdList().isEmpty()), "执行设备不能为空");
        Assert.notNull(rtce.getExecuteTime(), "执行时间不能为空");
        Assert.notNull(rtce.getExecuteData(), "执行数据不能为空");
        Assert.notNull(rtce.getStartTime(), "执行动作开始时间不能为空");
        Assert.notNull(rtce.getEndTime(), "执行动作结束时间不能为空");
        boolean addResult = service.add(rtce);
        if(!addResult){
            return RestBuilders.failureBuilder().message("失败").build();
        }
        return RestBuilders.successBuilder().message("成功").build();
    }

    @PostMapping("getPage")
    public RestMessage getPage(@RequestBody RequestModel<RegularTemperatureControlExt> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<RegularTemperatureControlExt> page = service.getPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(page).build();
    }

    @GetMapping("get/{id}")
    public RestMessage get(@PathVariable Long id) {
        Assert.notNull(id, "id 不能为空");
        RegularTemperatureControlExt regularTemperatureControlExt = service.getById(id);
        return RestBuilders.successBuilder().data(regularTemperatureControlExt).build();
    }

    @PostMapping("update")
    public RestMessage update(@RequestBody RegularTemperatureControlExt rtce) {
        Assert.isTrue(!StringUtils.isEmpty(rtce.getName()), "名称不能为空");
        Assert.isTrue(!(rtce.getDeviceIdList() == null || rtce.getDeviceIdList().isEmpty()), "执行设备不能为空");
        Assert.notNull(rtce.getExecuteTime(), "执行时间不能为空");
        Assert.notNull(rtce.getId(), "主键ID不能为空");
        Assert.notNull(rtce.getExecuteData(), "执行数据不能为空");
        Assert.notNull(rtce.getStartTime(), "执行动作开始时间不能为空");
        Assert.notNull(rtce.getEndTime(), "执行动作结束时间不能为空");
        boolean updateResult = service.update(rtce);
        if(!updateResult){
            return RestBuilders.failureBuilder().message("任务为禁用状态").build();
        }
        return RestBuilders.successBuilder().message("成功").build();
    }

    @PostMapping("batchUpdateStatus")
    public RestMessage batchUpdateStatus(@RequestBody List<RegularTemperatureControlExt> rtceList) {
        Assert.isTrue(!(rtceList == null || rtceList.isEmpty()), "批量更新数据不能为空");
        boolean bResult = service.batchUpdateStatus(rtceList);
        if(!bResult){
            return RestBuilders.failureBuilder().message("失败").build();
        }
        return RestBuilders.successBuilder().message("成功").build();
    }






}
